﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ViCode_LeVi.Data;
using System.Collections.ObjectModel;
using System.ComponentModel;
using ViCode_LeVi.Controls.Charts;
using ViCode_LeVi.Controls.Reports;
using System.Collections.Specialized;

namespace ViCode_LeVi.Controls
{
    /// <summary>
    /// Interaction logic for VMTabAnalysis_Tab1_Win.xaml
    /// </summary>
    public partial class VMTabAnalysis_Tab1_Win : UserControl
    {
        public VMTabAnalysis_Tab1_Win()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(VMTabAnalysis_Tab1_Win_Loaded);
            RefreshChart();            
        }
        
        void VMTabAnalysis_Tab1_Win_Loaded(object sender, RoutedEventArgs e)
        {
            BarChartImportance.Func_GetStep_TaskItemGroup = () =>
            {
                var codes = EnumControl_TaskType_Tab1.List_CheckBox.Where(c => c.IsChecked == true).Select(c => c.DataContext as Step_TaskItemGroup).Where(c => c != null).Select(c => c.Code);
                return RootData.Intance.Steps[3].TaskItemStep.TaskItemGroups_Source.Where(c => codes.Contains(c.Code)).ToArray();
            };
            Option_RunFirst();
            RefreshChart();
        }
        #region Option
        private void Option_RunFirst()
        {
            EnumControl_TaskType_Tab1.Option_Orientation = Orientation.Horizontal;
            EnumControl_TaskType_Tab1.Setup((t) => RefreshChart(), true);
            EnumControl_ImportanceMode_Tab1.Option_Orientation = Orientation.Vertical; 
            EnumControl_ImportanceMode_Tab1.Setup
                ((t) =>
                {
                        BarChartImportance.valuesbereich = (eImportanceMode)t;
                        RefreshChart();                        
                    }, false);
            ComboBox_ChartLabelMode_LoadData();
            ComboBox_ChartLegendMode_Setup();
        }
        //eImportanceMode valuesbereich
        private void ComboBox_ChartLegendMode_Setup()
        {
            ComboBox_ChartLegendMode.ItemsSource = Enum.GetValues(typeof(eChartLegendMode));
            ComboBox_ChartLegendMode.SelectedIndex = 0;

            BarChartImportance.ChartLegendMode = (eChartLegendMode)ComboBox_ChartLegendMode.SelectedValue;
            ComboBox_ChartLegendMode.SelectionChanged -= new SelectionChangedEventHandler(ComboBox_ChartLegendMode_SelectionChanged);
            ComboBox_ChartLegendMode.SelectionChanged += new SelectionChangedEventHandler(ComboBox_ChartLegendMode_SelectionChanged);

            BarChartImportance.LegendWithPercent = CheckBox_LegendWithPercent.IsChecked == true;
            CheckBox_LegendWithPercent.Click -= new RoutedEventHandler(CheckBox_LegendWithPercent_Click);
            CheckBox_LegendWithPercent.Click += new RoutedEventHandler(CheckBox_LegendWithPercent_Click);
        }

        void CheckBox_LegendWithPercent_Click(object sender, RoutedEventArgs e)
        {
            BarChartImportance.LegendWithPercent = CheckBox_LegendWithPercent.IsChecked == true;
            RefreshChart();
        }

        void ComboBox_ChartLegendMode_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ComboBox_ChartLegendMode.SelectedValue != null)
            {
                BarChartImportance.ChartLegendMode = (eChartLegendMode)ComboBox_ChartLegendMode.SelectedValue;
                RefreshChart();
            }
        }


        private void ComboBox_ChartLabelMode_LoadData()
        {
            ComboBox_ChartLabelMode.ItemsSource = Enum.GetValues(typeof(eChartLabelMode));
            ComboBox_ChartLabelMode.SelectedIndex = 0;

            BarChartImportance.ChartLabelMode = (eChartLabelMode)ComboBox_ChartLabelMode.SelectedValue;
            ComboBox_ChartLabelMode.SelectionChanged -= new SelectionChangedEventHandler(ComboBox_ChartLabelMode_SelectionChanged);
            ComboBox_ChartLabelMode.SelectionChanged += new SelectionChangedEventHandler(ComboBox_ChartLabelMode_SelectionChanged);

        }

        void ComboBox_ChartLabelMode_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ComboBox_ChartLabelMode.SelectedValue != null)
            {
                BarChartImportance.ChartLabelMode = (eChartLabelMode)ComboBox_ChartLabelMode.SelectedValue;
                RefreshChart();
            }
        }

        #endregion
        public void RefreshChart()
        {
            BarChartImportance.CreateChart();           
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ChartReport rpt = new ChartReport();
            rpt.Title = "Weight Bar Chart";
            rpt.AddChart(BarChartImportance);
            rpt.CreateReport(new StringCollection());
            ReportTemplate.ShowReportPreview(rpt);
        }
    }  
}
